package com.intct.hbase.dml;

import com.intct.util.ConnectionHBaseUtils;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025/6/13
 */
public class GetData {
    public static void main(String[] args) throws IOException {
        // 连接HBase
        Connection connection = ConnectionHBaseUtils.getConnection();

        // 获取Admin实例
        Admin admin = connection.getAdmin();

        // TabelName实例
//        TableName tableName = TableName.valueOf("INTCT_80_API", "T11");
        TableName tableName = TableName.valueOf("T21");

        // 获取Table实例
        Table table = connection.getTable(tableName);

        try {
            // 判断是否存在
            if (admin.tableExists(tableName)) {

                Get get = new Get(Bytes.toBytes("1002"));

                // 查询数据
                Result result = table.get(get);

                Cell[] cells = result.rawCells();

                for (Cell cell : cells) {
                    String rowKey = Bytes.toString(CellUtil.cloneRow(cell));
                    String family = Bytes.toString(CellUtil.cloneFamily(cell));
                    String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                    if (qualifier.equals("AGE")) {
                        int value = Bytes.toInt(CellUtil.cloneValue(cell));
                        System.out.println("row : " + rowKey + "; family : " + family + "; qualifier : " + qualifier + "; value : " + value);
                    } else {
                        String value = Bytes.toString(CellUtil.cloneValue(cell));
                        System.out.println("row : " + rowKey + "; family : " + family + "; qualifier : " + qualifier + "; value : " + value);
                    }

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (admin != null) {
                admin.close();
            }
            if (table != null) {
                table.close();
            }
        }

    }
}
